package com.fxwater.admin.controller.biz;

import java.util.List;

import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fxwater.admin.controller.system.SysProfileController;
import com.fxwater.biz.domain.FxwAlarmRecordInfo;
import com.fxwater.biz.domain.FxwDataInfo;
import com.fxwater.biz.domain.PumpStationInfo;
import com.fxwater.biz.domain.UnitInfo;
import com.fxwater.biz.service.IPumpStationService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import com.fxwater.common.utils.StringUtils;
import com.fxwater.common.utils.poi.ExcelUtil;
import com.fxwater.framework.util.ShiroUtils;
import com.fxwater.system.domain.SysNotice;
import com.fxwater.system.domain.SysUser;
import org.springframework.web.servlet.ModelAndView;

/**
 * 泵站管理
 * @author tx
 *
 */
@Controller
@RequestMapping("/biz/pumpStation")
public class PumpStationController extends BaseController {
	
	 private static final Logger logger = LoggerFactory.getLogger(PumpStationController.class);

	 private String prefix = "biz/pumpStation";
	 
	 @Autowired
	 private IPumpStationService pumpStationService ;
	 
	 
	 
	 /**
		 * 修改息状态
		 * @return
		 */
		@RequestMapping("/changeStatus")
		@RequiresPermissions("biz:pumpStation:edit")
		@ResponseBody
		public AjaxResult changeStatus(String id,String status) {
			PumpStationInfo pumpStationInfo = new PumpStationInfo();
			pumpStationInfo.setId(Long.parseLong(id));
			pumpStationInfo.setIsValid(status);
			return toAjax(pumpStationService.editSavePumpStationInfo(pumpStationInfo));
		}
	 
	 /**
	  * 跳转页面
	  * @return
	  */
	 @RequiresPermissions("biz:pumpStation:view")
	 @GetMapping()
	 public String pumpStation()
	 {
	     logger.info("泵站管理");
	     return prefix + "/pumpStation";
	  }
	 
	 /**
	  * 跳转数据页面
	  * @return
	  */
	 @RequiresPermissions("biz:station:dataView")
	 @GetMapping("/data")
	 public String pumpStationData()
	 {
	     return prefix + "/data";
	  }
	 
	 /**
	  * 泵站数据信息列表
	  * @param fxwDataInfo
	  * @return
	  */
	 @RequiresPermissions("biz:station:dataList")
	 @PostMapping("/data/dataList")
	 @ResponseBody
	 public TableDataInfo dataList (FxwDataInfo fxwDataInfo) {
		 startPage();
		 List<FxwDataInfo> list = pumpStationService.selectPumpStationInfoDataList(fxwDataInfo);
		 return getDataTable(list);
	 } 

	 
	 @Log(title = "泵站数据管理", businessType = BusinessType.EXPORT)
	 @RequiresPermissions("biz:station:dataExport")
	 @PostMapping("/data/dataExport")
	 @ResponseBody
	 public AjaxResult export(FxwDataInfo fxwDataInfo)
	 {
	     List<FxwDataInfo> list = pumpStationService.selectPumpStationInfoDataList(fxwDataInfo);
	     ExcelUtil<FxwDataInfo> util = new ExcelUtil<FxwDataInfo>(FxwDataInfo.class);
	     return util.exportExcel(list, "泵站数据");
	 }
	 
	 
	 @RequiresPermissions("biz:pumpStation:list")
	 @PostMapping("/list")
	 @ResponseBody
	 public TableDataInfo list (PumpStationInfo pumpStationInfo) {
		 startPage();
		 List<PumpStationInfo> list = pumpStationService.selectPumpStationInfoList(pumpStationInfo);
		 return getDataTable(list);
	 } 

	 /**
	  * 删除
	  * @return
	  */
	 @RequestMapping("/remove")
	 @ResponseBody
	 @RequiresPermissions("biz:pumpStation:remove")
	 @Log(title = "泵站删除", businessType = BusinessType.DELETE)
	 public AjaxResult remove(String ids) {
		 return toAjax(pumpStationService.removePumpStationInfo(ids));
	 }

	 /**
	  * 查看详情
	  * @return
	  */
	 @GetMapping("/detail/unit/{id}")
	 public String detail(@PathVariable("id") String id, ModelMap mmap) {
		 logger.info("====进入查看详情方法,传入的泵站表id是:" + id + "====");
		 PumpStationInfo pumpStationInfo = new PumpStationInfo();
		 pumpStationInfo.setId(Long.parseLong(id));
		 mmap.put("pumpStationInfo", pumpStationService.selectPumpStationInfoList(pumpStationInfo).get(0));
		 return prefix +"/detail";
	 }
	 
//	 @GetMapping("/detail/unit/{id}")
//	 public String detailUnit(@PathVariable("id") String id, ModelMap mmap) {
//		 PumpStationInfo pumpStationInfo = new PumpStationInfo();
//		 pumpStationInfo.setId(Long.parseLong(id));
//		 mmap.put("pumpStationInfo", pumpStationService.selectPumpStationInfoList(pumpStationInfo).get(0));
//		 return prefix +"/detail";
//	 }
//
	 @GetMapping("/detail/stationUserRelation/{id}")
	 public String detailStationUserRelation(@PathVariable("id") String id, ModelMap mmap) {
		 PumpStationInfo pumpStationInfo = new PumpStationInfo();
		 pumpStationInfo.setId(Long.parseLong(id));
		 mmap.put("pumpStationInfo", pumpStationService.selectPumpStationInfoList(pumpStationInfo).get(0));
		 return prefix +"/rela";
	 }
	 
	 @GetMapping("/detail/element/{id}")
	 public String detailElement(@PathVariable("id") String id, ModelMap mmap) {
		 PumpStationInfo pumpStationInfo = new PumpStationInfo();
		 pumpStationInfo.setId(Long.parseLong(id));
		 mmap.put("pumpStationInfo", pumpStationService.selectPumpStationInfoList(pumpStationInfo).get(0));
		 return prefix +"/element";
	 }
	 
	 @GetMapping("/detail/alerm/{id}")
	 public String alermElement(@PathVariable("id") String id, ModelMap mmap) {
		 PumpStationInfo pumpStationInfo = new PumpStationInfo();
		 pumpStationInfo.setId(Long.parseLong(id));
		 mmap.put("pumpStationInfo", pumpStationService.selectPumpStationInfoList(pumpStationInfo).get(0));
		 return prefix +"/alerm";
	 }
	 
	 @RequestMapping("/selectStationByName")
	 @ResponseBody
	 public AjaxResult selectStationByName(String name) {
		 PumpStationInfo station = new PumpStationInfo();
		 station.setPumpName(name);
		 List<PumpStationInfo> datas = pumpStationService.selectPumpStationInfoList(station);
		 return AjaxResult.success(datas);
	 }
	 
	 @RequestMapping("/selectStationNoUseByName")
	 @ResponseBody
	 public AjaxResult selectStationNoUseByName(String name) {
		 PumpStationInfo station = new PumpStationInfo();
		 station.setPumpName(name);
		 List<PumpStationInfo> datas = pumpStationService.selectStationNoUseByName(station);
		 return AjaxResult.success(datas);
	 }
	 
	 @GetMapping("/selectPumpStation")
	 public String selectPumpStation( ModelMap mmap)
	 {
//	     mmap.put("station", pumpStationService.selectDeptById(deptId));
		 mmap.put("flag", "");
	     return prefix + "/station";
	  }
	 
	 /**
	  * 查看没有告警规则的泵站信息
	  * @param mmap
	  * @return
	  */
	 @GetMapping("/selectPumpStationNoUse")
	 public String selectPumpStationNoUse(String alerFlag, ModelMap mmap)
	 {
	     mmap.put("flag", alerFlag);
	     return prefix + "/station";
	  }
	 
	 
	 /**
	  * 增加泵站
	  * @param mmap
	  * @return
	  */
	 @GetMapping("/add")
	 public String add(ModelMap mmap) {
		 return prefix + "/add";
	 }
	 
	 /**
	  * 添加泵站
	  * @param pumpStationInfo
	  * @return
	  */
	 @RequiresPermissions("biz:pumpStation:add")
	 @Log(title = "泵站管理", businessType = BusinessType.INSERT)
	 @PostMapping("/add")
	 @ResponseBody
	 public AjaxResult addSave(PumpStationInfo pumpStationInfo)
	 {
		 pumpStationInfo.setCreateBy(ShiroUtils.getLoginName());//
	     return toAjax(pumpStationService.addPumpSatation(pumpStationInfo));
	 }

	 /**
	  * 跳转修改页面
	  * @param mmap
	  * @return
	  */
	 @GetMapping("edit/{stationId}")
	 public String edit ( ModelMap mmap,@PathVariable("stationId") String stationId) {
		 PumpStationInfo pumpStationInfo = new PumpStationInfo();
		 pumpStationInfo.setId(Long.parseLong(stationId));
		 pumpStationInfo =  pumpStationService.selectPumpStationInfoList(pumpStationInfo).get(0);
		 mmap.addAttribute("pumpStationInfo", pumpStationInfo);
		 return prefix + "/edit";
	 }
	 
	 /**
	  * 保存泵站修改信息
	  * @return
	  */
	 @RequiresPermissions("biz:pumpStation:edit")
	 @Log(title = "泵站管理", businessType = BusinessType.UPDATE)
	 @PostMapping("edit")
	 @ResponseBody
	 public AjaxResult editSave(PumpStationInfo pumpStationInfo) {
		 pumpStationInfo.setWaterArea(null);
		 return toAjax(pumpStationService.editSavePumpStationInfo(pumpStationInfo));
	 }

	/**
	 * 保存供水区域
	 *
	 */
	@RequestMapping(value = "saveWaterArea",method = RequestMethod.POST,produces={"application/json"})
	@ResponseBody
	public AjaxResult saveWaterArea(@RequestBody JSONObject jsonParam){
		String waterArea=jsonParam.getString("waterArea");
		long id=Long.parseLong(jsonParam.getString("id"));
		logger.info("====供水区域传入参数是:" + jsonParam + "====");
		PumpStationInfo pumpStationInfo = new PumpStationInfo();
		pumpStationInfo.setWaterArea(waterArea);
		pumpStationInfo.setId(id);
		int flag = pumpStationService.editSavePumpStationInfo(pumpStationInfo);
		logger.info("====保存供水区域的结果是：" + flag + "====");
		return toAjax(flag);
	}

	/**
	 * 查询泵站信息
	 */
	@RequestMapping(value = "stationList",method = RequestMethod.POST,produces={"application/json"})
	@ResponseBody
	public List<PumpStationInfo> stationList(){
		List<PumpStationInfo> pumpStationInfoList = pumpStationService.selectPumpStationInfoList(null);
		return pumpStationInfoList;
	}

}
